import type { AxiosResponse, ApiResponse, ApiResponseSuccess, ApiResponseError } from 'axios';

/**
 * 判断给定的 ApiResponse 对象是否为 ApiResponseSuccess 类型
 */
export function apiResponseIsSuccess<T = any>(data: ApiResponse<T>): data is ApiResponseSuccess<T> {
  return data.success === true;
}

/**
 * 判断给定的 ApiResponse 对象是否为 ApiResponseError 类型
 */
export function apiResponseIsError(data: ApiResponse): data is ApiResponseError {
  return !apiResponseIsSuccess(data);
}

/**
 * 判断任意类型对象是否为 AxiosResponse 类型
 */
export function isAxiosResponse(data: any): data is AxiosResponse {
  return 'status' in data && 'statusText' in data && 'data' in data && 'config' in data;
}

/**
 * 判断响应是否为 AxiosApiResponse 类型
 * @param response 响应对象
 * @returns 如果响应数据为 ApiResponse 类型，则返回 true，否则返回 false
 */
export function responseIsAxiosApiResponse(
  response: AxiosResponse,
): response is AxiosResponse<ApiResponse> {
  return 'success' in response.data && 'msg' in response.data;
}

/**
 * 判断任意类型是否为AxiosApiResponseError类型的响应
 */
export function isAxiosApiResponseError(data: any): data is AxiosResponse<ApiResponseError> {
  return isAxiosResponse(data) && responseIsAxiosApiResponse(data) && apiResponseIsError(data.data);
}

/**
 * 判断任意类型是否为AxiosApiResponseSuccess类型的响应
 */
export function isAxiosApiResponseSuccess(data: any): data is AxiosResponse<ApiResponseSuccess> {
  return isAxiosResponse(data) && responseIsAxiosApiResponse(data) && apiResponseIsError(data.data);
}
